<template>
  <div class="app-container search-table-box aidex-table">
    <el-card shadow="never" ref="queryRef" style="margin-bottom: 12px;" class="search_card" v-show="showSearch">
      <div class="filter-container">
        <div class="search_box">
          <el-form :model="queryParams" ref="queryForm" label-width="100px" size="mini">
            <el-row :gutter="2">
              <el-col :md="8" >
                <el-form-item prop="searchValues">
                  <el-select
                    v-model="queryParams.searchValues"
                    multiple
                    filterable
                    remote
                    reserve-keyword
                    clearable
                    placeholder="请输入货物及服务编码/货物及服务名称"
                    :remote-method="remoteSearchValue"
                    @change="handleQuery"
                    :loading="remoteSearchValueLoading"
                    style="width: 415px;margin-left: -78px"
                  >
                    <el-option
                      v-for="item in searchValueOptions"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                      :disabled="item.disabled">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :md="3">
                <el-form-item label="显示已停用"  prop="isEnabled">
                  <el-checkbox v-model="queryParams.isEnabled"></el-checkbox>
                </el-form-item>
              </el-col>
              <el-col :md="3">
                <el-form-item label="仅显示默认商品"  prop="isDefault">
                  <el-checkbox v-model="queryParams.isDefault"></el-checkbox>
                </el-form-item>
              </el-col>
              <el-col :md="6" align="right">
                <el-form-item>
                  <el-button class="filter-item" type="primary" @click="handleQuery">搜索</el-button>
                  <el-button class="filter-item" style="margin-left: 8px" @click="resetQuery">重置</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </div>
    </el-card>
    <el-card shadow="never" >
      <template #header>
        <el-row>
          <el-col :span="8">
            <div class="card-header">
              <el-button disabled type="text">商品信息</el-button>
            </div>
          </el-col>
          <el-col :span="16">
            <div class="btn_box" align="right" style="float: right;">
              <el-button
                class="filter-item"
                style="margin-left: 8px;"
                type="primary"
                icon="el-icon-plus"
                @click="handleAdd"
                size="mini"
                v-hasPermi="['saleconfig:product:add']"
              >手工新增</el-button>
              <el-button
                class="filter-item"
                style="margin-left: 8px;"
                icon="el-icon-delete"
                type="danger"
                :disabled="multiple"
                @click="handleDelete"
                size="mini"
                v-if="!multiple"
                v-hasPermi="['saleconfig:product:remove']"
              >删除</el-button>
              <el-button
                class="filter-item"
                style="margin-left: 8px;"
                icon="el-icon-download"
                @click="handleExport"
                size="mini"
                v-hasPermi="['saleconfig:product:export']"
              >导出</el-button>
              <right-toolbar :showSearch.sync="showSearch" :columns.sync="columns" :columnsKey.sync="columnsKey" @showSearchFun="showSearchFun" @queryTable="getList" :tableKey.sync="tableKey"></right-toolbar>
            </div>
          </el-col>
        </el-row>
      </template>

      <el-table
              border
              stripe
              default-expand-all
              ref="tableRef"
              v-loading="loading"
              :data="productList"
              @selection-change="handleSelectionChange"
              :row-style="rowClass"
              :header-cell-style= "{
                'font-size': '14px !important',
                'font-weight': '500 !important',
                'color': 'rgb(0 0 0)',
                'background-color': 'rgb(205 224 255)',
              }"
              highlight-current-row style="width: 100%;"
              :height="tableHeight"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" type="index" align="center" width="55">
          <template slot-scope="scope">
            <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <template v-for="item in columns">
          <el-table-column label="货物及服务编码" align="center" prop="productCode"  v-if="item.visible && item.label=='货物及服务编码'" width="150" show-overflow-tooltip/>
          <el-table-column label="货物及服务名称" align="center" prop="productName"  v-if="item.visible && item.label=='货物及服务名称'" width="150" show-overflow-tooltip/>
          <el-table-column label="规格型号" align="center" prop="specificatCode"  v-if="item.visible && item.label=='规格型号'" width="140" show-overflow-tooltip/>
          <el-table-column label="单位" align="center" prop="unit"  v-if="item.visible && item.label=='单位'" show-overflow-tooltip/>
          <el-table-column label="单价" align="center" prop="price"  v-if="item.visible && item.label=='单价'" show-overflow-tooltip/>
          <el-table-column label="含税标识" align="center" prop="isTaxName"  v-if="item.visible && item.label=='含税标识'" show-overflow-tooltip/>
          <el-table-column label="税收分类" align="center" prop="taxName"  v-if="item.visible && item.label=='税收分类'" width="180" show-overflow-tooltip/>
          <el-table-column label="享受优惠政策" align="center" prop="isPolicyName"  v-if="item.visible && item.label=='享受优惠政策'" width="160" show-overflow-tooltip/>
          <el-table-column label="优惠政策类型" align="center" prop="policyName"  v-if="item.visible && item.label=='优惠政策类型'" width="160" show-overflow-tooltip/>
          <el-table-column label="税率" align="center" prop="taxRate"  v-if="item.visible && item.label=='税率'">
            <template slot-scope="scope">
              <span>{{scope.row.taxRate}}%</span>
            </template>
          </el-table-column>
          <el-table-column label="免税类型" align="center" prop="freeTaxTypeName"  v-if="item.visible && item.label=='免税类型'" width="160" show-overflow-tooltip/>
          <el-table-column label="即征即退" align="center" prop="refundTaxTypeName"  v-if="item.visible && item.label=='即征即退'" show-overflow-tooltip/>
          <el-table-column label="备注" align="center" prop="remark"  v-if="item.visible && item.label=='备注'" show-overflow-tooltip/>
          <el-table-column label="是否默认" align="center" prop="isDefaultName"  v-if="item.visible && item.label=='是否默认'" show-overflow-tooltip>
          </el-table-column>
          <el-table-column label="启用" align="center" prop="isEnabledName"  v-if="item.visible && item.label=='启用'" show-overflow-tooltip>
          </el-table-column>
          <el-table-column label="更新人" align="center" prop="updateBy"  v-if="item.visible && item.label=='更新人'" show-overflow-tooltip/>
          <el-table-column label="更新日期" align="center" prop="updateTime" width="180" v-if="item.visible && item.label=='更新日期'" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
        </template>


        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120" fixed="right">
          <template slot-scope="scope">
<!--            <el-button-->
<!--              type="text"-->
<!--              @click="handleTop(scope.row)"-->
<!--            >置顶</el-button>-->
<!--            <el-divider direction="vertical"></el-divider>-->
            <el-button
              type="text"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['saleconfig:product:edit']"
            >编辑</el-button>
          </template>
        </el-table-column>
        <div slot="empty">
          <svg-icon icon-class="search-none" style="font-size: 64px;" />
          <p>暂无数据</p>
        </div>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改商品对话框 -->
      <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="920px" top="10vh" append-to-body>
        <div style="height: 490px;overflow: auto; padding: 12px 24px;">
          <el-form ref="form" size="mini" :model="form" :rules="rules" label-position="right"  label-width="140px">
            <el-row :gutter="24">
              <el-col :span="12">
                <el-form-item label="货物及服务编码" prop="productCode">
                  <el-input v-model="form.productCode" placeholder="请输入货物及服务编码" style="width: 250px"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="货物及服务名称" prop="productName">
                  <el-input v-model="form.productName" placeholder="请输入货物及服务名称"  style="width: 250px"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="规格型号" prop="specificatCode">
                  <el-input v-model="form.specificatCode" placeholder="请输入规格型号"  style="width: 250px"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="单位" prop="unit">
                  <el-input v-model="form.unit" placeholder="请输入单位"  style="width: 250px"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="单价" prop="price">
                  <el-input v-model="form.price" placeholder="请输入单价"  style="width: 250px"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="含税标识" prop="isTax" >
                  <el-select v-model="form.isTax" placeholder="请选择" clearable filterable style="width: 250px" >
                    <el-option
                      v-for="dict in dict.type.is_tax"
                      :key="dict.value"
                      :label="dict.label"
                      :value="parseInt(dict.value)"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="税收分类编码" prop="taxCode">
                  <el-input v-model="form.taxCode" maxlength = 0 clearable placeholder="请选择" style="width: 270px" @clear="handleClearTaxCode()">
                    <el-button slot="append" @click="openTaxCodeDialogOpen">
                      智能赋码
                    </el-button>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="税收分类名称" prop="taxName">
                  <el-input v-model="form.taxName" placeholder="自动带出"  style="width: 250px" readonly/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="是否享受优惠政策标识" prop="isPolicy">
                  <el-select v-model="form.isPolicy" placeholder="请选择" clearable filterable style="width: 250px" >
                    <el-option
                      v-for="dict in dict.type.is_policy"
                      :key="dict.value"
                      :label="dict.label"
                      :value="parseInt(dict.value)"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="优惠政策类型" prop="policyCode">
                  <el-select v-model="form.policyCode" placeholder="请选择" clearable filterable style="width: 250px" :disabled="form.isPolicy!='1'">
                    <el-option
                      v-for="dict in dict.type.policy_code"
                      :key="dict.value"
                      :label="dict.label"
                      :value="parseInt(dict.value)"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="税率" prop="taxRate">
                  <el-select v-model="form.taxRate" placeholder="请选择" clearable filterable style="width: 250px">
                    <el-option
                      v-for="dict in dict.type.tax_rate"
                      :key="dict.label"
                      :label="dict.label"
                      :value="parseInt(dict.value)"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="免税类型" prop="freeTaxType">
                  <el-select v-model="form.freeTaxType" placeholder="请选择" clearable filterable style="width: 250px">
                    <el-option
                      v-for="dict in dict.type.free_tax_type"
                      :key="dict.value"
                      :label="dict.label"
                      :value="parseInt(dict.value)"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="即征即退类型" prop="refundTaxType">
                  <el-select v-model="form.refundTaxType" placeholder="请选择" clearable filterable style="width: 250px">
                    <el-option
                      v-for="dict in dict.type.refund_tax_type"
                      :key="dict.value"
                      :label="dict.label"
                      :value="parseInt(dict.value)"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="备注" prop="remark">
                  <el-input v-model="form.remark" placeholder="请输入备注"  style="width: 250px"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="启用" prop="isEnabled">
                  <el-radio-group v-model="form.isEnabled">
                  <el-radio :label="1">启用</el-radio>
                  <el-radio :label="0">禁用</el-radio>
                </el-radio-group>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="是否默认" prop="isDefault">
                  <el-switch v-model="form.isDefault"
                             :active-value="1"
                             :inactive-value="0"> </el-switch>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary"  size="mini" @click="submitForm">确 定</el-button>
          <el-button  size="mini" @click="cancel">取 消</el-button>
        </div>
      </el-dialog>

      <!-- 查询条件-公司-弹出查询 -->
      <el-dialog v-dialogDrag  :close-on-click-modal="false" title="查询条件-公司查询" :visible.sync="searchsiteCodeDialogOpen" width="800px" append-to-body>
        <div class="dialog_box" style="height:550px;">
            <el-form label-position="left" :model="searchsiteCodeQueryParams" :inline="true"  ref="searchsiteCodeQueryForm" label-width="88px" >
                <el-form-item  prop="searchsiteCodeSearchValue">
                    <el-input
                            v-model="searchsiteCodeQueryParams.searchValue"
                            size="mini"
                            placeholder="请输入编码或名称查询公司"
                            @keyup.enter.native="handleSearchsiteCodeQuery"
                            clearable
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleSearchsiteCodeQuery">查询</el-button>
                </el-form-item>
            </el-form>
            <el-table v-loading="searchsiteCodeSelectLoading" :data="searchsiteCodeList"
                      height="400"
                      @row-click="handleSearchsiteCodeRowClick"
                      ref="searchsiteCodeMultipleTable"
                      :row-key="getSearchsiteCodeListRowKey"
                      :row-style="{height:2+'px'}"
                      :header-cell-style= "{
                        'font-size': '14px !important',
                        'font-weight': '500 !important',
                        'background-color':'rgb(241 241 241 / 52%)'
                      }"
                      :cell-style="{padding:'2px 4px'}"
                      border
                      @selection-change="handleSearchsiteCodeSelectionChange">
                <el-table-column type="selection" width="55" align="center"/>
                <!-- 数据 -->
                <el-table-column prop="siteCode" label="组织编码" width="180"></el-table-column>
                <el-table-column prop="siteName" label="组织名称" ></el-table-column>
            <el-table-column prop="propertyTypeName" label="性质" width="150"></el-table-column>
            </el-table>
            <pagination
                    v-show="searchsiteCodeTotal>0"
                    :total="searchsiteCodeTotal"
                    :page.sync="searchsiteCodeQueryParams.pageNum"
                    :limit.sync="searchsiteCodeQueryParams.pageSize"
                    @pagination="getSearchsiteCodeList"
            />
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button size="mini" type="primary" @click="confirmSearchsiteCode">确 定</el-button>
            <el-button size="mini"   @click="searchsiteCodeCancel">取 消</el-button>
        </div>
    </el-dialog>

      <!-- 编辑-公司-弹出查询 -->
      <el-dialog v-dialogDrag  :close-on-click-modal="false" title="编辑商品-公司选择" :visible.sync="siteCodeDialogOpen" width="800px" append-to-body>
        <div class="dialog_box" style="height:550px;">
            <el-form label-position="left" :model="siteCodeQueryParams" :inline="true"  ref="siteCodeQueryForm" label-width="88px" >
                <el-form-item  prop="siteCodeSearchValue">
                    <el-input
                            v-model="siteCodeQueryParams.searchValue"
                            size="mini"
                            placeholder="请输入编码或名称查询公司"
                            @keyup.enter.native="handlesiteCodeQuery"
                            clearable
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handlesiteCodeQuery">查询</el-button>
                </el-form-item>
            </el-form>
            <el-table v-loading="siteCodeSelectLoading" :data="siteCodeList"
                      height="400"
                      @row-click="handlesiteCodeRowClick"
                      ref="siteCodeSingleTable"
                      :row-key="getsiteCodeListRowKey"
                      :row-style="{height:2+'px'}"
                      :header-cell-style= "{
                        'font-size': '14px !important',
                        'font-weight': '500 !important',
                        'background-color':'rgb(241 241 241 / 52%)'
                      }"
                      :cell-style="{padding:'2px 4px'}"
                      border
                      @selection-change="handlesiteCodeSelectionChange">
                <el-table-column label="选择" width="60">
            　　　　<template slot-scope="scope">
            　　　　　　<el-radio v-model="siteCodeDialogRadio" :label="scope.row"><i></i></el-radio>
            　　　　</template>
            　　 </el-table-column>
                <!-- 数据 -->
                <el-table-column prop="companycode" label="公司代码" width="180"></el-table-column>
                <el-table-column prop="companyname" label="公司名称" ></el-table-column>
                <el-table-column prop="remark" label="备注" width="120"></el-table-column>
            </el-table>
            <pagination
                    v-show="siteCodeTotal>0"
                    :total="siteCodeTotal"
                    :page.sync="siteCodeQueryParams.pageNum"
                    :limit.sync="siteCodeQueryParams.pageSize"
                    @pagination="getsiteCodeList"
            />
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button size="mini" type="primary" @click="confirmsiteCode">确 定</el-button>
            <el-button size="mini" @click="siteCodeCancel">取 消</el-button>
        </div>
    </el-dialog>

      <!-- 编辑-税收分类编码-弹出查询 -->
      <el-dialog v-dialogDrag  :close-on-click-modal="false" title="税收分类编码选择" :visible.sync="taxCodeDialogOpen" width="800px" append-to-body>
        <div class="dialog_box" style="height:550px;">
          <el-form label-position="left" :model="taxCodeQueryParams" :inline="true"  ref="taxCodeQueryForm" label-width="88px" >
            <el-form-item  prop="taxCodeSearchValue">
              <el-input
                v-model="taxCodeQueryParams.searchValue"
                size="mini"
                placeholder="请输入税收编码/名称/简称"
                @keyup.enter.native="handleTaxCodeQuery"
                clearable
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleTaxCodeQuery">查询</el-button>
            </el-form-item>
          </el-form>
          <el-table v-loading="taxCodeSelectLoading" :data="taxCodeList"
                    height="400"
                    @row-click="handleTaxCodeRowClick"
                    ref="taxCodeSingleTable"
                    :row-key="getTaxCodeListRowKey"
                    :row-style="{height:2+'px'}"
                    :header-cell-style= "{
                        'font-size': '14px !important',
                        'font-weight': '500 !important',
                        'color':'#FFF',
                        'padding': '0px 0',
                        'background-color':'rgb(51 112 255 / 80%)',
                        textAlign: 'left',
                        'height': '30px !important'
                      }"
                    :cell-style="{padding:'2px 4px'}"
                    border
                    @selection-change="handleTaxCodeSelectionChange">
            <el-table-column label="选择" width="60">
        　　　　<template slot-scope="scope">
        　　　　　　<el-radio v-model="taxCodeDialogRadio" :label="scope.row"><i></i></el-radio>
        　　　　</template>
        　　 </el-table-column>
            <!-- 数据 -->
            <el-table-column prop="taxName" label="税收分类名称" width="200px" show-overflow-tooltip></el-table-column>
            <el-table-column prop="taxShortName" label="简称"  width="160px" show-overflow-tooltip></el-table-column>
            <el-table-column prop="taxCategoryCode" label="税收分类编码" width="200" show-overflow-tooltip></el-table-column>
            <el-table-column prop="taxRateName" label="税率" width="120" show-overflow-tooltip></el-table-column>
            <el-table-column prop="policyDesc" label="享受优惠政策" width="180" show-overflow-tooltip></el-table-column>
            <el-table-column prop="policyName" label="优惠政策类型" width="180" show-overflow-tooltip></el-table-column>
          </el-table>
          <pagination
            v-show="siteCodeTotal>0"
            :total="siteCodeTotal"
            :page.sync="siteCodeQueryParams.pageNum"
            :limit.sync="siteCodeQueryParams.pageSize"
            @pagination="getsiteCodeList"
          />
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button size="mini" type="primary" @click="confirmTaxCode">确 定</el-button>
          <el-button size="mini" @click="taxCodeCancel">取 消</el-button>
        </div>
      </el-dialog>



    </el-card>
  </div>
</template>

<script>
import { listProduct, getProduct, delProduct, addProduct, updateProduct } from "@/api/saleconfig/product";

import { listCompany as listcompany, getCompany as getcompany } from "@/api/core/company";
/* 城市列表 **/
import { provinceList, cityList, districtList } from "@/api/system/city";

import { listTaxcategory, getTaxcategory, delTaxcategory, addTaxcategory, updateTaxcategory } from "@/api/saleconfig/taxcategory";

export default {
  name: "Product",
  dicts: ['policy_code', 'tax_rate', 'free_tax_type', 'refund_tax_type', 'is_policy', 'is_tax'],

    data() {
    return {
      advanced: false,
      tableHeight: this.getInitTableHeight(),
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        params:{

        },
        searchValues: [],
        siteCode: null,
        productCode: null,
        productName: null,
        specificatCode: null,
        unit: null,
        price: null,
        isTax: null,
        isTaxName: null,
        taxCode: null,
        taxName: null,
        isPolicy: null,
        isPolicyName: null,
        policyCode: null,
        policyName: null,
        taxRate: null,
        taxRateName: null,
        freeTaxType: null,
        freeTaxTypeName: null,
        refundTaxType: null,
        refundTaxTypeName: null,
        isEnabled: null,
        isEnabledName: null,
        isDefault: null,
        isDefaultName: null,
        createName: null,
        updateBy: null,
        updateTime: null
      },
      // 远程查询条件加载
      remoteSearchValueLoading: false,
      // 关键字查询条件
      searchValueOptions:[

      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      tableKey: 4,
      //
      columnsKey: 'product',
      // 自定义列
      columns: [
          {
              key: 0,
              label: `货物及服务编码`,
              visible: true
          },
          {
              key: 1,
              label: `货物及服务名称`,
              visible: true
          },
          {
              key: 2,
              label: `规格型号`,
              visible: true
          },
          {
              key: 3,
              label: `单位`,
              visible: true
          },
          {
              key: 4,
              label: `单价`,
              visible: true
          },
          {
              key: 5,
              label: `含税标识`,
              visible: true
          },
          {
              key: 6,
              label: `税收分类编码`,
              visible: true
          },
          {
              key: 7,
              label: `税收分类名称`,
              visible: true
          },
          {
              key: 8,
              label: `是否享受优惠政策`,
              visible: true
          },
          {
              key: 9,
              label: `优惠政策类型`,
              visible: true
          },
          {
              key: 10,
              label: `税率`,
              visible: true
          },
          {
              key: 11,
              label: `免税类型`,
              visible: true
          },
          {
              key: 12,
              label: `即征即退类型`,
              visible: true
          },
          {
              key: 13,
              label: `备注`,
              visible: true
          },
          {
              key: 14,
              label: `是否默认`,
              visible: true
          },
          {
              key: 15,
              label: `更新人`,
              visible: true
          },
          {
              key: 16,
              label: `更新日期`,
              visible: true
          },
          {
              key: 17,
              label: `启用`,
              visible: true
          },
      ],
      /* ======================================省市区======================================  */
      // 省
      provinceId: null,
      provinceList:[],
      // 市
      cityId: null,
      cityList:[],
      // 区
      districtId: null,
      districtList:[],

      /* ======================================查询条件-公司代码选择======================================  */
      searchsiteCodes:[],
      searchsiteCodenames:[],
      searchsiteCodeTotal: 0,
      searchsiteCodeList: [],
      searchsiteCodeDialogOpen: false,
      searchsiteCodeSelectLoading: false,
      searchsiteCodeQueryParams: {
          searchValue: null,
          pageNum: 1,
          pageSize: 20,
      },
      /* ======================================编辑-公司代码选择======================================  */
      siteCodename: null,
      siteCodeTotal: 0,
      siteCodeList: [],
      siteCodeDialogRadio: null,
      siteCodeDialogOpen: false,
      siteCodeSelectLoading: false,
      siteCodeQueryParams: {
          searchValue: null,
          pageNum: 1,
          pageSize: 20,
      },

      /* ======================================查询条件-税收分类编码选择======================================  */
      taxCodes:[],
      taxCodeNames:[],
      taxCodeTotal: 0,
      taxCodeList: [],
      taxCodeDialogRadio: null,
      taxCodeDialogOpen: false,
      taxCodeSelectLoading: false,
      taxCodeQueryParams: {
          searchValue: null,
          pageNum: 1,
          pageSize: 20,
      },

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 行样式 */
    rowClass({ row, rowIndex }) {
      if (this.ids.includes(row.id)) {
          return { background: "#d9edf7", height:'20px' };
      } else {
          return { height:'20px'};
      }
    },
   /** 隐藏搜索按钮操作 */
   showSearchFun(isShowSearch){
      this.showSearch = isShowSearch
      let oldHeight = this.$refs.queryRef.$el.offsetHeight
      if(!isShowSearch){
          //当前是显示状态
          oldHeight = oldHeight + 12
      }else{
          oldHeight = oldHeight - 12
      }
      this.$nextTick(() => (
         this.tableHeight = this.$refs.tableRef.$el.offsetHeight - (this.$refs.queryRef.$el.offsetHeight-oldHeight)
      ))
    },
    /** 展开按钮操作 */
    toggleAdvanced() {
      const oldHeight = this.$refs.queryRef.$el.offsetHeight
      this.advanced = !this.advanced
      this.$nextTick(() => (
         this.tableHeight = this.$refs.tableRef.$el.offsetHeight - (this.$refs.queryRef.$el.offsetHeight-oldHeight)
      ))
    },
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productId: null,
        custId: null,
        siteCode: null,
        siteName: null,
        productCode: null,
        productName: null,
        specificatCode: null,
        unit: null,
        price: null,
        isTax: null,
        isTaxName: null,
        taxCode: null,
        taxName: null,
        isPolicy: null,
        isPolicyName: null,
        policyCode: null,
        policyName: null,
        taxRate: null,
        taxRateName: null,
        freeTaxType: null,
        freeTaxTypeName: null,
        refundTaxType: null,
        refundTaxTypeName: null,
        remark: null,
        isEnabled: null,
        isEnabledName: null,
        isDefault: null,
        isDefaultName: null,
        createName: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },


      /* ※※ 查询 省市区 相关方法 start ※※ */
      // 获取省
      getProvinceList(){
          provinceList().then(response => {
              this.provinceList = response.data;
          });
      },
      // 获取市
      getCityList(){
          console.log(this.provinceId);
          cityList( {"parentId" : this.provinceId } ).then(response => {
              this.cityList = response.data;
          });
      },
      // 获取区县
      getDistrictList(){
          districtList( {"parentId" : this.cityId } ).then(response => {
              this.districtList = response.data;
          });
      },
      // 选中省份
      handleChangeProvince(val){
          // 给表单省字段赋值
          this.form.provinceid = val;
          let province = {};
          province = this.provinceList.find((item)=>{
              return item.id === val;
          });
          this.form.province = province.name;
          // 城市数据清空、区县数据清空;
          this.cityList = [];
          this.districtList = [];
          // 已选市、区数据清空
          this.cityId = null;
          this.districtId = null;
          // 表单城市、区县数据清空
          this.form.cityid = null;
          this.form.city = null;
          this.form.areaid = null;
          this.form.area = null;
          // 获取城市数据
          this.getCityList();
      },
      // 清空省份
      handleClearProvince(){
          // 城市数据清空、区县数据清空;
          this.cityList = [];
          this.districtList = [];
          // 已选市、区数据清空
          this.cityId = null;
          this.districtId = null;
          // 表单城市、区县数据清空
          this.form.cityid = null;
          this.form.city = null;
          this.form.areaid = null;
          this.form.area = null;
      },
      // 选中城市
      handleChangeCity(val){
          // 给表单市字段赋值
          this.form.cityid = val;
          let city = {};
          city = this.cityList.find((item)=>{
              return item.id === val;
          });
          this.form.city = city.name;
          // 区县数据清空
          this.districtList = [];
          // 已选区县清空
          this.districtId = null;
          // 表单区县清空
          this.form.areaid = null;
          this.form.area = null;
          // 获取城市数据
          this.getDistrictList();
      },
      // 清空省份
      handleClearCity(){
          // 区县数据清空
          this.districtList = [];
          // 已选区县清空
          this.districtId = null;
          // 表单区县清空
          this.form.areaid = null;
          this.form.area = null;
      },
      // 选中区县
      handleChangeDistrict(val){
          // 给表单省字段赋值
          this.form.areaid = val;
          let district = {};
          district = this.districtList.find((item)=>{
              return item.id === val;
          });
          this.form.area = district.name;
      },
      // 清空省份
      handleClearDistrict(){
          // 表单区县清空
          this.form.areaid = null;
          this.form.area = null;
      },

      /* ※※ 查询 省市区 相关方法 end ※※ */




    /* ※※ 查询 公司代码 相关方法 start ※※ */
    /** ① 弹出查询条件-公司代码 */
    openSearchsiteCodeDialogOpen(){
        this.searchsiteCodeDialogOpen = true;
        // 默认不查询
    },
    /** ② 单击行 **/
    handleSearchsiteCodeRowClick(row, column, event){
        this.$refs.searchsiteCodeSingleTable.toggleRowSelection(row);
    },
    /** ③ 行KEY **/
    getSearchsiteCodeListRowKey(row) {
        return row.companycode
    },
    /** ④ 选中公司代码 **/
    handleSearchsiteCodeSelectionChange(selection) {
        this.searchsiteCodes = selection.map(item => item.siteCode);
        this.searchsiteCodenames = selection.map(item => item.siteName);
    },
    /** ⑤ 点击查询按钮 **/
    handleSearchsiteCodeQuery(){
        this.searchsiteCodeQueryParams.pageNum = 1;
        this.getSearchsiteCodeList();
    },
    /** ⑥ 查询 **/
    getSearchsiteCodeList(){
        this.searchsiteCodeSelectLoading = true;
        listcompany(this.searchsiteCodeQueryParams).then(response => {
            this.searchsiteCodeList = response.rows;
            this.searchsiteCodeTotal = response.total;
            this.searchsiteCodeSelectLoading = false;
        });
    },
    /** ⑦ 点击确认查询 **/
    confirmSearchsiteCode() {
        if(this.searchsiteCodes.length > 0){
            this.queryParams.params.siteCodes = this.searchsiteCodes.join(',');
            this.queryParams.params.companynames = this.searchsiteCodenames.join(',');
        }
        // 关闭查询弹出框
        this.searchsiteCodeCancel();
    },
    /** ⑧ 点击关闭查询弹出框 **/
    searchsiteCodeCancel() {
        this.searchsiteCodeDialogOpen = false;
    },
    /* ※※ 查询 公司代码 相关方法 end ※※ */


    /* ※※ 编辑 公司代码 相关方法 start ※※ */
    /** ① 弹出编辑内容-公司代码 */
    opensiteCodeDialogOpen(){
        this.siteCodeDialogOpen = true;
        this.getsiteCodeList();
    },
    /** ② 单击行 **/
    handlesiteCodeRowClick(row, column, event){
        this.$refs.siteCodeSingleTable.toggleRowSelection(row);
    },
    /** ③ 行KEY **/
    getsiteCodeListRowKey(row) {
        return row.companycode
    },
    /** ④ 选中公司代码 **/
    handlesiteCodeSelectionChange(selection) {
        if (selection[0]) {
            this.siteCodeDialogRadio = selection[0]
            this.form.companycode = selection[0].companycode;
            this.form.companyname = selection[0].companyname;
        }
    },
    /** ⑤ 点击查询按钮 **/
    handlesiteCodeQuery(){
        this.siteCodeQueryParams.pageNum = 1;
        this.getsiteCodeList();
    },
    /** ⑥ 查询 **/
    getsiteCodeList(){
        this.siteCodeSelectLoading = true;
        listcompany(this.siteCodeQueryParams).then(response => {
            this.siteCodeList = response.rows;
            this.siteCodeTotal = response.total;
            this.siteCodeSelectLoading = false;
        });
    },
    /** ⑦ 点击确认查询 **/
    confirmsiteCode() {
        this.siteCodeCancel();
    },
    /** ⑧ 点击关闭查询弹出框 **/
    siteCodeCancel() {
        this.siteCodeDialogOpen = false;
    },
    /* ※※ 编辑 公司代码 相关方法 end ※※ */


    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProduct(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品";
      });
    },
    /** 置顶 */
    handleTop(row) {

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProduct(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品编号为"' + ids + '"的数据项？').then(function() {
        return delProduct(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('saleconfig/product/export', {
        ...this.queryParams
      }, `product_${new Date().getTime()}.xlsx`)
    },
    /** 动态加载关键字查询条件 */
    remoteSearchValue(query) {
        this.remoteSearchValueLoading = true;
        if (query !== '') {
            this.searchValueOptions = [
                {value: '货物及服务编码:' + query ,label: '货物及服务编码:' + query },
                {value: '货物及服务名称:' + query ,label: '货物及服务名称:' + query },
            ];
            this.searchValueOptions.map(item => {
                this.queryParams.searchValues.map((item2, index) => {
                    if(item.value.startsWith('货物及服务编码') && item2.startsWith('货物及服务编码')){
                        item.disabled = true;
                    }
                    if(item.value.startsWith('货物及服务名称') && item2.startsWith('货物及服务名称')){
                        item.disabled = true;
                    }
                })
            });
        } else {
            this.searchValueOptions = [];
        }
        this.remoteSearchValueLoading = false;
    },


    /* ※※ 编辑 税收分类编码 相关方法 start ※※ */
    /** ① 弹出编辑内容-税收分类编码 */
    openTaxCodeDialogOpen(){
        this.taxCodeDialogOpen = true;
        this.getTaxCodeList();
    },
    /** ② 单击行 **/
    handleTaxCodeRowClick(row, column, event){
        this.$refs.taxCodeSingleTable.toggleRowSelection(row);
    },
    /** ③ 行KEY **/
    getTaxCodeListRowKey(row) {
        return row.taxCode
    },
    /** ④ 选中公司代码 **/
    handleTaxCodeSelectionChange(selection) {
        if (selection[0]) {
            this.taxCodeDialogRadio = selection[0]
            this.form.taxCode = selection[0].taxCategoryCode.trim();
            this.form.taxName = selection[0].taxName.trim();
        }
    },
    /** ⑤ 点击查询按钮 **/
    handleTaxCodeQuery(){
        this.taxCodeQueryParams.pageNum = 1;
        this.getTaxCodeList();
    },
    /** ⑥ 查询 **/
    getTaxCodeList(){
        this.taxCodeSelectLoading = true;
        listTaxcategory(this.taxCodeQueryParams).then(response => {
            this.taxCodeList = response.rows;
            this.taxCodeTotal = response.total;
            this.taxCodeSelectLoading = false;
        });
    },
    /** ⑦ 点击确认查询 **/
    confirmTaxCode() {
        this.taxCodeCancel();
    },
    /** ⑧ 点击关闭查询弹出框 **/
    taxCodeCancel() {
        this.taxCodeDialogOpen = false;
    },
    /* ※※ 编辑 公司代码 相关方法 end ※※ */

    handleClearTaxCode() {
        this.form.taxCode = null;
        this.form.taxName = null;
    }
  }
};
</script>
